﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class DAO_TaiKhoan
    {
       
        public static List<DTO_TaiKhoan> DangNhap( string tentk, string matkhau)
        {
            List<DTO_TaiKhoan> tk = new List<DTO_TaiKhoan>();
            try
            {
                string s = "Proc_SelectTaiKhoan";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                cmd.Parameters.Add("@TenTK", SqlDbType.NVarChar).Value = tentk;
                cmd.Parameters.Add("@MatKhau", SqlDbType.NVarChar).Value = matkhau;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_TaiKhoan taikhoan = new DTO_TaiKhoan();
                    taikhoan.TenTK = dt.Rows[i]["TenTK"].ToString();
                    taikhoan.MatKhau = dt.Rows[i]["MatKhau"].ToString();
                    taikhoan.ID_Nhom = int.Parse(dt.Rows[i]["ID_Nhom"].ToString());
                    tk.Add(taikhoan);
                }
                ketnoi.Cmb().Close();
                return tk;
            }
            catch (Exception ex)
            {
                return tk;
            }

        }
        public static bool deleteTaiKhoanTheoMaSV(string masv)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_deleteTaiKhoanTheoTenTK";
                SqlCommand cmd = new SqlCommand(s,cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@TenTK", masv);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd.ExecuteNonQuery();
                ketnoi.Cmb().Close();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public static List<DTO_TaiKhoan> SelectTaiKhoan()
        {
            List<DTO_TaiKhoan> listTaiKhoan = new List<DTO_TaiKhoan>();
            {
                try
                {
                    string s = "Proc_SelectTaiKhoan_QLTK";
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = ketnoi.Cmb();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = s;
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DTO_TaiKhoan TK = new DTO_TaiKhoan();
                        TK.TenTK = dt.Rows[i]["TenTK"].ToString();
                        TK.MatKhau = dt.Rows[i]["MatKhau"].ToString();
                        TK.ID_Nhom = int.Parse(dt.Rows[i]["ID_Nhom"].ToString());
                        listTaiKhoan.Add(TK);
                    }
                    ketnoi.Cmb().Close();
                    return listTaiKhoan;
                }

                catch (Exception ex)
                {
                    return listTaiKhoan;
                }
            }

        }
        public static bool insertTaiKhoan(DTO_TaiKhoan TaiKhoan)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_InsertTaiKhoan";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@TenTK", TaiKhoan.TenTK);
                cmd1.Parameters.AddWithValue("@MatKhau", TaiKhoan.MatKhau);
                cmd1.Parameters.AddWithValue("@ID_Nhom", TaiKhoan.ID_Nhom);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static bool updateTaiKhoan(DTO_TaiKhoan TaiKhoan, string TenTK_cu)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_UpdateTaiKhoan";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@TenTK", TaiKhoan.TenTK);
                cmd1.Parameters.AddWithValue("@MatKhau", TaiKhoan.MatKhau);
                cmd1.Parameters.AddWithValue("@ID_Nhom", TaiKhoan.ID_Nhom); ;
                cmd1.Parameters.AddWithValue("@TenTK_cu", TenTK_cu);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static bool UpdateTaiKhoan_MatKhau(DTO_TaiKhoan TaiKhoan, string MatKhau_cu)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_UpdateTaiKhoan_MatKhau";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@MatKhau", TaiKhoan.MatKhau);
                cmd1.Parameters.AddWithValue("@MatKhau_cu", MatKhau_cu);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static bool InsertDangNhap_VaoTaiKhoan(DTO_TaiKhoan tk)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_InsertDangNhap_VaoTaiKhoan";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@TenTK", tk.TenTK);
                cmd1.Parameters.AddWithValue("@MatKhau", tk.@MatKhau);
                cmd1.Parameters.AddWithValue("@ID_Nhom", tk.ID_Nhom);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

    }
}
